import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-08-15
 * Time: 19:49
 */
public class jdbc {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        //创建数据源, 找到数据库服务器
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java117?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("201754ljj");
        //与数据库服务器建立连接
        Connection connection = dataSource.getConnection();
        //构造SQL语句
        String sql = "insert into student values(?, ?)"; //占位符, 占参数
        int id = scanner.nextInt();
        String name = scanner.next();
        PreparedStatement preparedStatement = connection.prepareStatement(sql); //SQL演示可能出现错误, 抛异常
        preparedStatement.setInt(1, id);
        preparedStatement.setString(2, name);
        //执行SQL语句
        int n = preparedStatement.executeUpdate();
        System.out.println("n = " + n);
        //关闭连接服务
        preparedStatement.close();
        connection.close();

    }
}
